<template>
  <div class="money">
    <van-nav-bar
  title="我的钱包"
  left-arrow
  @click-left="back"
/>
<!-- 新增签到区域 -->
    <div class="sign-in-section">
      <button @click="doSignIn" :disabled="isSignedIn">{{ isSignedIn? '今日已签到' : '签到' }}</button>
    </div>
    <div class="header">
      <div v-if="!isPhoneNumberBound" class="phone-warning">
        您还未绑定手机号，为了您的账户安全，请立刻
        <button @click="bindPhoneNumber">绑定手机号</button>
      </div>
      <section class="balance-section">
        <h5>我的余额</h5>
        <p>{{ balance }}.00</p>
        <div>
           <!-- 提现按钮，点击触发提现Dialog -->
      <button class="withdraw" @click="openWithdrawDialog">提现</button>
      <!-- 充值按钮，点击触发充值Dialog -->
      <button class="recharge" @click="openRechargeDialog">充值</button>
        </div>
      </section>
       <section class="child-coins-section">
        <h5>我的微币</h5>
        <p>{{ childCoins }}.00</p>
        <button class="buyChildCoins" @click="openBuyChildCoinsDialog">购买</button>
      </section>
    </div>
      <section class="record-section">
        <van-cell @click="showAssetDetails" title="资产明细" is-link />
        <van-cell @click="showDetails('consumption')" title="消费记录" is-link/>
        <van-cell @click="showDetails('reward')" title="打赏记录" is-link/>
      </section>
        <!-- 资产明细展示区域 -->
    <div v-if="showAssetDetailsVisible" class="asset-details-section">
      <h2>资产明细列表</h2>
      <ul>
        <li v-for="(detail, index) in assetDetails" :key="index" class="detail-item">
          <div class="detail-header">
            <span class="time">{{ detail.time }}</span>
            <span class="type">{{ detail.type }}</span>
            <span class="amount">{{ detail.amount }}</span>
            <button @click="toggleDetailExpand(index)" class="expand-button">
              {{ isDetailExpanded[index]? '收起' : '展开' }}
            </button>
          </div>
          <div v-if="isDetailExpanded[index]" class="detail-body">
            <p>备注：{{ detail.remark }}</p>
            <p>相关订单号：{{ detail.orderNumber }}</p>
            <!-- 可以根据实际情况添加更多详细信息展示 -->
          </div>
        </li>
      </ul>
    </div>
     <!-- 提现-->
      <van-dialog v-model:show="showwithdraw" @confirm="performWithdraw" title="提现" show-cancel-button="">
    <transition name="dialog-fade">
      <div class="dialog-overlay">
        <div class="dialog-content">
          <h3>提现金额</h3>
       <span>￥</span><input type="number" v-model="withdrawInput" placeholder="输入提现金额" />
        </div>
      </div>
    </transition>
</van-dialog>
    <!-- 充值-->
      <van-dialog v-model:show="showrecharge" @confirm="performRecharge" title="充值" show-cancel-button>
    <transition name="dialog-fade">
      <div class="dialog-overlay">
        <div class="dialog-content">
          <h3>充值金额</h3>
          <span>￥</span><input type="number" v-model="rechargeInput" placeholder="输入充值金额" />
        </div>
      </div>
    </transition>
    </van-dialog>
     <!-- 购买儿童币-->
      <van-dialog @confirm="performBuyChildCoins" v-model:show="showbuyChildCoins" title="购买" show-cancel-button>
    <transition name="dialog-fade">
      <div class="dialog-overlay">
        <div class="dialog-content">
          <h3>购买儿童币</h3>
          <span>￥</span><input type="number" v-model="buyChildCoinsInput" placeholder="输入购买金额" />
        </div>
      </div>
    </transition>
    </van-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showSuccessToast, showFailToast } from 'vant';
const disabled = ref(false);
const router = useRouter();
const back = () => {
    router.go(-1);
}
const showwithdraw = ref(false); // 控制提现对话框的显示与隐藏
const showrecharge = ref(false); // 控制充值对话框的显示与隐藏
const showbuyChildCoins = ref(false); // 控制购买对话框的显示与隐藏
// 响应式数据
const balance = ref(0);
const childCoins = ref(0);
const isPhoneNumberBound = ref(false);
const isSignedIn = ref(false);
const queren=()=>{
}
// 控制资产明细展示区域是否可见
const showAssetDetailsVisible = ref(false);
// 模拟的资产明细数据
const assetDetails = ref([
  {
    time: '2024-11-10 10:00:00',
    type: '充值',
    amount: '100',
    remark: '微信充值',
    orderNumber: '20241110001',
  },
  {
    time: '2024-11-12 15:30:00',
    type: '消费',
    amount: '-50',
    remark: '购买商品',
    orderNumber: '20241112002',
  },
]);

// 显示资产明细方法
const showAssetDetails = () => {
    showAssetDetailsVisible.value = true;
};

// 用于记录每条明细是否展开的数组
const isDetailExpanded = ref(Array(assetDetails.value.length).fill(false));

// 提现
const withdrawInput = ref(0);
const openWithdrawDialog = () => {
  showwithdraw.value = true;
};
const closeWithdrawDialog = () => {
  showwithdraw.value = false;
};
const performWithdraw = () => {
  if (balance.value >= withdrawInput.value) {
    balance.value -= withdrawInput.value;
    closeWithdrawDialog();
    showSuccessToast('提现成功');
    // 提现成功后清空输入框
    withdrawInput.value = 0;
  } else {
    showFailToast('余额不足');
    withdrawInput.value = 0;
    showwithdraw.value = false;
  }
};

// 充值
const rechargeInput = ref(0);
const openRechargeDialog = () => {
  showrecharge.value = true;
};
const closeRechargeDialog = () => {
  showrecharge.value = false;
};
const performRecharge = () => {
  balance.value += rechargeInput.value;
  closeRechargeDialog();
  showSuccessToast('充值成功');
  rechargeInput.value = 0;
};

// 购买儿童币
const buyChildCoinsInput = ref(0);
const openBuyChildCoinsDialog = () => {
  showbuyChildCoins.value = true;
};
const closeBuyChildCoinsDialog = () => {
  showbuyChildCoins.value = false;
};
const performBuyChildCoins = () => {
  if (balance.value >= buyChildCoinsInput.value) {
    balance.value -= buyChildCoinsInput.value;
    childCoins.value += buyChildCoinsInput.value;
    closeBuyChildCoinsDialog();
    showSuccessToast('购买成功');
    // 购买成功后清空输入框
    buyChildCoinsInput.value = 0;
  } else {
    showFailToast('余额不足');
    buyChildCoinsInput.value = 0;
    showbuyChildCoins.value = false;
  }
};


// 绑定手机号方法
const bindPhoneNumber = () => {
  const phoneNumber = prompt('请输入手机号');
  if (phoneNumber) {
    isPhoneNumberBound.value = true;
  }
};

// 显示记录详情方法
const showDetails = (type) => {
  switch (type) {
    case 'asset':
      alert('查看资产明细');
      break;
    case 'consumption':
      alert('查看消费记录');
      break;
    case'reward':
      alert('查看打赏记录');
      break;
  }
};
// 签到方法
const doSignIn = () => {
  if (!isSignedIn.value) {
    // 这里可以添加签到逻辑，例如给余额或儿童币增加一定数量，或者记录签到天数等
    // 简单示例，给余额增加1元
    showSuccessToast('签到成功');
    balance.value += 1;
    isSignedIn.value = true;
  }
};
// 切换明细展开收起的方法
const toggleDetailExpand = (index) => {
  isDetailExpanded.value[index] =!isDetailExpanded.value[index];
};
// 显示消费记录方法
const showConsumptionDetails = () => {
  alert('查看消费记录');
};

// 显示打赏记录方法
const showRewardDetails = () => {
  alert('查看打赏记录');
};
</script>

<style scoped>
.money {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  border-radius: 0.3rem;
  box-shadow: 0 0 0.3rem rgba(0, 0, 0, 0.1);
  position: relative;
}

.header {
  width: 100%;
  height: 2.5rem;
  background: orange;
}

.phone-warning {
  width: 100%;
  height: .3rem;
  background: #fdedaa;
  font-size: .12rem;
  text-align: center;
  line-height: .3rem;
}
.phone-warning button{
    border: none;
    background: none;
    color: blue;
}
.balance-section {
  width: 100%;
  height: 1rem;
  background: #fc5557;
  border: .01rem solid yellow;
    position: relative;
}
.balance-section h5{
    font-size: .15rem;
    color: #fec697;
    padding-top: .15rem;
    margin-left: .5rem;
}
.balance-section p {
  font-size: .3rem;
  color: #fec697;
  margin-top: .1rem;
  margin-left: .5rem;
}
.withdraw{
    width: .8rem;
    height: .3rem;
    background: #fd5757;
    border: .02rem solid #ffb8b3;
    border-radius: .2rem;
    color: white;
    position: absolute;
    top: .15rem;
    right: 1rem;
}
.recharge{
    width: .8rem;
    height: .3rem;
    background: #fd5757;
    border: .02rem solid #ffb8b3;
    border-radius: .2rem;
    color: white;
    position: absolute;
    top: .15rem;
    right: .1rem;
}
.balance-buttons {
  display: flex;
  justify-content: center;
  margin-top: 0.6rem;
}

.balance-buttons button {
  background-color: white;
  color: #ff9800;
  border: none;
  padding: 0.3rem 0.6rem;
  border-radius: 0.1rem;
  cursor: pointer;
  margin-right: 0.3rem;
}

.child-coins-section {
   width: 100%;
  height: 1.2rem;
  background: #fc5557;
  position: relative;
}
.child-coins-section h5{
    font-size: .15rem;
    color: #fec697;
    padding-top: .15rem;
    margin-left: .5rem;
}
.child-coins-section p {
  font-size: .3rem;
  color: #fec697;
  margin-top: .1rem;
  margin-left: .5rem;
}
.buyChildCoins{
    width: .8rem;
    height: .3rem;
    background: #fd5757;
    border: .02rem solid #ffb8b3;
    border-radius: .2rem;
    color: white;
    position: absolute;
    top: .4rem;
    right: .1rem;
}
.record-section{
    width: 100%;
    height: 1.4rem;
    background: #fff;
    margin-top: .15rem;
}
/* 新增签到区域样式 */
.sign-in-section {
    width: 1rem;
    height: 0.5rem;
  position: absolute;
  top: .05rem;
  right: .05rem;
  z-index: 1;
}
.sign-in-section button{
  border: none;
  color: red;
}
input{
  width:1.5rem;
  height: .3rem;
  text-align: center;
  /* background: red; */
  margin-left: .1rem;
  border: .01rem solid #ccc;
  border-radius: .2rem;
  margin-top: .15rem;
}
.dialog-content button{
  width: 1rem;
  height: .25rem;
  border:none;
  margin-left: .1rem;
}
.dialog-content h3{
  margin-left: .2rem;
}
.dialog-content span{
  margin-left: .6rem
}
/* dialog-content */
</style>